/**
 * @auther senhu chen
 * interview
 */
app.controller('interController', ['$scope', '$rootScope', '$state', '$uibModal', '$window', 'interInterviewService', 'sendOnboardingService', 'pagingFilter', 'filterFilter', 'slideCallbackService', function($scope, $rootScope, $state, $uibModal, $window, interInterviewService, sendOnboardingService, pagingFilter, filterFilter, slideCallbackService) {
	$scope.currPage = 1;
	$scope.totalItems;
	$scope.itemsPer = 15;
	if($window.innerHeight < 800) {
		$scope.itemsPer = 10;
	}
	
	interInterviewService().then(function(result) {
		$scope.$watch('interKey', function(newValue, oldValue) {
			var reqresult = filterFilter(result, newValue);
			pagingFilter.listLoad(reqresult, $scope.itemsPer);
			$scope.totalItems = pagingFilter.getPgInfo().resArylength;
			$scope.interviews = pagingFilter.getCurrentPage($scope.currPage);
		});
	}, function() {
		
	});
	
	$scope.doTask = function(inter, $event) {
		if(inter.drmsDemandRequest == null) {
			$state.go('recrut2.interview.twom.slide', {interId: inter.id, reqid: null, status: inter.status});
		} else {
			$state.go('recrut2.interview.twom.slide', {interId: inter.id, reqid: inter.drmsDemandRequest.demandShowId, status: inter.status});
		}
		$event.stopPropagation();
		$event.preventDefault();
	}
	
	$scope.doUpdate = function(inter, $event){
		$rootScope.interUpdate = inter;
		$state.go('recrut2.interview.update');
		$event.stopPropagation();
		$event.preventDefault();
	}
	
	$scope.doSend = function(inter, $event){
		console.log(inter);
		sendOnboardingService(inter).then(function(result){
			if(result == 1){
				var uibModal = $uibModal.open({
					animation : false,
					templateUrl : './views/sendSuccess.html',
					size : 'lg',
					controller : ['$scope','$uibModalInstance',function($scope, $uibModalInstance) {
						$scope.ok = function(event) {
							$uibModalInstance.close();
							event.preventDefault();
							event.stopPropagation();
						}
					}]
				});
			}else{
				var uibModal = $uibModal.open({
					animation : false,
					templateUrl : './views/sendFail.html',
					size : 'lg',
					controller : ['$scope','$uibModalInstance',function($scope, $uibModalInstance) {
						$scope.ok = function(event) {
							$uibModalInstance.close();
							event.preventDefault();
							event.stopPropagation();
						}
					}]
				});
			}
		});
	}
	
	$scope.$watch('interKey', function(newValue, oldValue) {
		interInterviewService().then(function(result) {
			var reqresult = filterFilter(result, newValue);
			pagingFilter.listLoad(reqresult, $scope.itemsPer);
			$scope.totalItems = pagingFilter.getPgInfo().resArylength;
			$scope.interviews = pagingFilter.getCurrentPage($scope.currPage);
		})
	});
	
	$scope.$watch('currPage', function(newValue, oldValue) {
		$scope.interviews = pagingFilter.getCurrentPage(newValue);
	});
	
	slideCallbackService.getPromise.then(function(result) {
		console.log('compinent resolve' + result);
	}, function(result) {
		console.log('compinent reject' + result);
	}, function(result) {
		interInterviewService().then(function(result) {
			$scope.$watch('interKey', function(newValue, oldValue) {
				var reqresult = filterFilter(result, newValue);
				pagingFilter.listLoad(reqresult, $scope.itemsPer);
				$scope.totalItems = pagingFilter.getPgInfo().resArylength;
				$scope.interviews = pagingFilter.getCurrentPage($scope.currPage);
			});
		}, function() {
			
		});
	});
}]);

/**
 * InterviewInfo Slide
 */
app.controller('interControllerSlide', ['$scope', '$state', '$uibModal', '$stateParams', 'validNotifyService', 'interInfoService', 'interInfoAddService', 'statusAddService', 'slideCallbackService', function($scope, $state, $uibModal, $stateParams, validNotifyService, interInfoService, interInfoAddService, statusAddService, slideCallbackService) {
	var dmdShowId = $stateParams.reqid;
	var status = $stateParams.status;
	var interId = $stateParams.interId;
	interInfoService(interId).then(function(result){
		$scope.interviewInfos = result;
	}, function(){
		
	});
	
	/**
	 * interviewComments detail
	 */
	$scope.commentDetail = function(comment) {
		var $ctrl = this;
		$ctrl.comment = comment;
		$uibModal.open({
			animation: false,
			templateUrl: './views/intermodal.html',
			size: 'lg',
			controller: ['$scope', '$uibModalInstance', 'comment', function($scope, $uibModalInstance, comment) {
				$scope.comment = comment;
				$scope.ok = function(event) {
					$uibModalInstance.close();
					event.preventDefault();
					event.stopPropagation();
				}
			}],
			resolve: {
				comment: function() {
					return $ctrl.comment;
				}
			}
		});
	}
	
	$scope.inter={
		id: interId,
	    drmsDemandRequest: {
	      id: 0,
	      demandShowId: dmdShowId
	    },
	    drmsCandidate: {
	      candId: 0,
	      candShowId: null,
	      candName: null
	    },
	    status: status,
	    mainRejectReason: null,
	    otherRejectReason: null,
	    drmsInterviewInfo: {
	      id:0,
		  interviewType: null,
		  interviewDate: null,
		  interviewer: null,
		  interviewResult: null,
		  interviewComments: null
	    }
	};
	
	/**
	 * interviewInfoAdd
	 */
	$scope.addInterInfo = function() {
		var interInfos = [];
		if($scope.inter.drmsInterviewInfo.interviewType == null
		 ||$scope.inter.drmsInterviewInfo.interviewDate == null
		 ||$scope.inter.drmsInterviewInfo.interviewer == null
		 ){
			validNotifyService.promiseDeferd.notify();
		} else {
			interInfos.push($scope.inter);
			interInfoAddService.interviews($scope.inter).then(function() {
				interInfoService(interId).then(function(result){
					$scope.interviewInfos = result;
					$scope.inter.drmsInterviewInfo.interviewType = null;
					$scope.inter.drmsInterviewInfo.interviewDate = null;
					$scope.inter.drmsInterviewInfo.interviewer = null;
					$scope.inter.drmsInterviewInfo.interviewResult = null;
					$scope.inter.drmsInterviewInfo.interviewComments = null;
				}, function(){
					
				});
			});
		}
	}
	
	/**
	 * Submit
	 */
	$scope.subInter = function(){
		var inters = [];
		if($scope.inter.status == null
		 ||$scope.inter.drmsDemandRequest.demandShowId == null){
			validNotifyService.promiseDeferd.notify();
		} else {
			inters.push($scope.inter);
			statusAddService.interviews($scope.inter).then(function() {			
				slideCallbackService.promiseDeferd.notify('promise noitfy');
				$state.go('recrut2.interview');
			});
		}
	}
	
	$scope.updateInterInfo = function(interInfo) {
		$scope.inter.drmsInterviewInfo.id = interInfo.id;
		$scope.inter.drmsInterviewInfo.interviewer = interInfo.interviewer;
		$scope.inter.drmsInterviewInfo.interviewType = interInfo.interviewType;
		$scope.inter.drmsInterviewInfo.interviewDate = interInfo.interviewDate;
		$scope.inter.drmsInterviewInfo.interviewResult = interInfo.interviewResult;
		$scope.inter.drmsInterviewInfo.interviewComments = interInfo.interviewComments;
	}
}]);

/**
 * Interview add
 */
app.controller('interControllerAdd', ['$scope', '$state','interAddService', 'slideCallbackService', 'validNotifyService', function($scope,$state, interAddService, slideCallbackService, validNotifyService){
	$scope.inter={
		id: 0,
	    drmsDemandRequest: {
	      id: 0,
	      demandShowId: null
	    },
	    drmsCandidate: {
	      candId: 0,
	      candShowId: null,
	      candName: null
	    },
	    status: null,
	    mainRejectReason: null,
	    otherRejectReason: null,
	    drmsInterviewInfo: {
	      id:0,
		  interviewType: null,
		  interviewDate: null,
		  interviewer: null,
		  interviewResult: null,
		  interviewComments: null
	    }
	};
	
	$scope.addInter = function(){
		var inters = [];
		if($scope.inter.drmsDemandRequest.demandShowId == null
		 ||$scope.inter.drmsCandidate.candName == null
		 ||$scope.inter.status == null
		 ||$scope.inter.mainRejectReason == null
		 ||$scope.inter.otherRejectReason == null
		 ||$scope.inter.drmsInterviewInfo.interviewType == null
		 ||$scope.inter.drmsInterviewInfo.interviewDate == null
		 ||$scope.inter.drmsInterviewInfo.interviewer == null
		 ||$scope.inter.drmsInterviewInfo.interviewResult == null
		 ||$scope.inter.drmsInterviewInfo.interviewComments == null
		 ){
			validNotifyService.promiseDeferd.notify();
		} else {
			inters.push($scope.inter);
			interAddService.interviews($scope.inter).then(function() {
				slideCallbackService.promiseDeferd.notify('promise noitfy');
				$state.go('recrut2.interview');
			});
		}
	}
}]);

app.controller('interControllerUpdate', ['$scope', '$rootScope', '$state', 'interUpdateService', 'findCandNameService', 'slideCallbackService', 'validNotifyService', function($scope, $rootScope, $state, interUpdateService, findCandNameService, slideCallbackService, validNotifyService){
	var interUpdate = $rootScope.interUpdate;
	var candName;
	if(interUpdate.drmsDemandRequest == null){
		var dmdShowId = null;
	}else{
		var dmdShowId = interUpdate.drmsDemandRequest.demandShowId;
	}
	if(interUpdate.drmsCandidate == null){
		var candShowId = null;
	}else{
		var candShowId = interUpdate.drmsCandidate.candShowId;
	}
	
	$scope.$watch('inter.drmsCandidate.candShowId', function(newValue, oldValue) {
		findCandNameService(newValue).then(function(result) {
			if(result.candShowId != null){
				$scope.inter.drmsCandidate.candName = result.candShowId;
			}else{
				$scope.inter.drmsCandidate.candName = null;
			}
		})
	});
	
	$scope.inter={
		id: interUpdate.id,
	    drmsDemandRequest: {
	      id: 0,
	      raiseDate: null,
	      isflp: null,
	      hiringReason: null,
	      refName: null,
	      language: null,
	      primarySkill: null,
	      secondarySkill: null,
	      targetJoinDate: null,
	      status: null,
	      demandShowId: dmdShowId
	    },
	    drmsCandidate: {
	      candId: 0,
	      candShowId: candShowId,
	      candName: null,
	      recruiter: null,
	      resource: null,
	      resume: null,
	      contactDate: null,
	      currentLocation: null,
	      telphone: null,
	      email: null,
	      year: null,
	      currentCompany: null,
	      currentSalary: null,
	      dep: null,
	      position: null,
	      status: null,
	      comments: null,
	      domain: null,
	      skills: null,
	      language: null
	    },
	    status: interUpdate.status,
	    mainRejectReason: interUpdate.mainRejectReason,
	    otherRejectReason: interUpdate.otherRejectReason,
	    drmsInterviewInfo: {
	      id:0,
		  interviewType: null,
		  interviewDate: null,
		  interviewer: null,
		  interviewResult: null,
		  interviewComments: null
	    }
	};
	
	$scope.editSub = function(){
		var inters = [];
		if($scope.inter.drmsDemandRequest.demandShowId == null
		 ||$scope.inter.drmsCandidate.candShowId == null
		 ||$scope.inter.status == null
		 ){
			validNotifyService.promiseDeferd.notify();
		} else {
			inters.push($scope.inter);
			interUpdateService.interUpdate($scope.inter).then(function() {
				slideCallbackService.promiseDeferd.notify('promise noitfy');
				$state.go('recrut2.interview');
			});
		}
	}
}]);

/**
 * Status Select
 */
app.controller("stusCtrl", ["$scope",function($scope) {
	$scope.status = ["Technical Interview Scheduled", "Technical Interview Passed", "Technical Interview Failed","Technical Interview Cancelled",
	    "Proposed to Manager Interview","Manager Assessment", "Manager Interview Scheduled", "Manager Interview Passed","Manager Interview Failed",
		"Manager Interview Cancelled","Proposed to Client Interview","Client Assessment","Client Interview Scheduled","Client Interview Passed",
		"Client Interview Failed","Client Interview Cancelled","Offer Negotiation","Offer Proposed","Offer Process","Offer Accepted","Onboarding Process",
		"Hired","On Hold","Not Selected","Rejected"];
}]);

/**
 * InterviewType Select
 */
app.controller("typeCtrl", ["$scope",function($scope) {
	$scope.types = ["General Interview", "Technical Interview", "Hiring Manger Interview","Client Interview"];
}]);

/**
 * InterviewResult Select
 */
app.controller("resultCtrl", ["$scope",function($scope) {
	$scope.results = ["Pass", "Reject", "Pending", "Cancel", "Propose to other position"];
}]);

app.controller("dmdShowIdCtrl", ["$scope", "findDmdShowIdService", function($scope, findDmdShowIdService){
	findDmdShowIdService().then(function(result){
		$scope.dmdShowIds = result;
	}, function(){
		
	});
}]);

app.controller("candShowIdCtrl", ["$scope", "findCandShowIDService", function($scope, findCandShowIDService){
	findCandShowIDService().then(function(result){
		$scope.candShowIds = result;
	}, function(){
		
	});
}]);